<?php
	namespace Application\Admin\Controller;
	use Application\Admin\Model\Auth as AuthModel;
	use KJ\Page;
	
	class Auth extends Admin{
		
		public function index(){
			
		$auth = new AuthModel();

		$data = $auth-> select();			
								
		$count = count($data);
		
		$page = new Page($data, $count, 10);
		
		$data = $page->pagination();
		
		$this->data = $data;															

		$this -> render();
			
		}
		
	public function add() {
		if (IS_POST) {
			$auth = new AuthModel();
			if($_POST['name'] == '' || $_POST['href'] == ''){
				echo "<script>alert('请输入信息');
					window.location.href='index.php?m=admin&c=auth&a=add';</script>";
				exit ;
			}
			$auth -> attributes -> name = $_POST['name'];
			$auth -> attributes -> value = $_POST['href'];
			if ($auth -> save()) {
				echo "<script>alert('添加成功');
					window.location.href='index.php?m=admin&c=auth&a=index';</script>";
				exit ;
			}
		}
		$this -> render('add');
	}
	
	public function delete() {
		if (IS_GET) {
			$id = $_GET['id'];
			$auth = new AuthModel();
			$auth = $auth -> findModel($id);
			if ($auth -> delete()) {
				echo "<script>alert('删除成功');
					window.location.href='index.php?m=admin&c=auth&a=index';</script>";
			}
		}
	}	
	
	public function edit() {
		if (IS_GET) {
			$id = $_GET['id'];
			$auth = new AuthModel();
			$this -> auth = $auth -> find($id);
			$this -> render('edit');
		}
		if (IS_POST) {
			$auth = new AuthModel();
			$id = $_POST['id'];
			$auth = $auth -> findModel($id);
			$auth -> attributes -> name = $_POST['name'];
			$auth -> attributes -> value = $_POST['href'];
			if ($auth -> save()) {
				echo "<script>alert('编辑成功');
					window.location.href='index.php?m=admin&c=auth&a=index';</script>";
			}
		}
	}
		
	}
?>